package cn.wen.yaling.xo.service.impl;

import cn.wen.yaling.commons.constant.Constants;
import cn.wen.yaling.commons.constant.RedisConstants;
import cn.wen.yaling.commons.entity.ExceptionLogEntity;
import cn.wen.yaling.commons.enums.LogStatus;
import cn.wen.yaling.commons.service.RedisService;
import cn.wen.yaling.commons.utils.*;
import cn.wen.yaling.commons.vo.ExceptionLogVO;
import cn.wen.yaling.commons.vo.LogQueryVO;
import cn.wen.yaling.commons.vo.LogVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.wen.yaling.xo.dao.LogDao;
import cn.wen.yaling.commons.entity.LogEntity;
import cn.wen.yaling.xo.service.LogService;


@Service("logService")
public class LogServiceImpl extends ServiceImpl<LogDao, LogEntity> implements LogService {

    @Autowired
    private LogDao logDao;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LogEntity> page = this.page(
                new Query<LogEntity>().getPage(params),
                new QueryWrapper<LogEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public ResultEntity getLogList() {
        List<LogVO> logList;
        // 1、先判断redis缓存中是否能命中缓存
        if (redisService.hasKey(RedisConstants.CACHE_ADMIN_HANDLER_LOG_LIST)) {
            logList = redisUtils.getListCache(RedisConstants.CACHE_ADMIN_HANDLER_LOG_LIST, LogVO.class);
        } else {
            // 2、缓存不命中则到db中获取
            QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("log_status", LogStatus.NORMAL_USE);
            queryWrapper.orderByDesc("create_time");
            List<LogEntity> entities = logDao.selectList(queryWrapper);
            if (entities.size() == 0) return ResultEntity.error();
            logList = entities.stream().map((item) -> {
                LogVO target = new LogVO();
                BeanUtils.copyProperties(item, target);
                return target;
            }).collect(Collectors.toList());
            // 3、将db中的缓存获取到
            redisUtils.setListCache(RedisConstants.CACHE_ADMIN_HANDLER_LOG_LIST, logList);
        }
        return ResultEntity.ok().put("data", logList);
    }

    @Override
    public ResultEntity searchLogList(LogQueryVO logQueryVO) {
        QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(logQueryVO.getNickname())) {
            queryWrapper.like("nickname", logQueryVO.getNickname().trim());
        }

        if (StringUtils.isNotBlank(logQueryVO.getHandleDes())) {
            queryWrapper.like("handle_des", logQueryVO.getHandleDes());
        }

        if (StringUtils.isNotBlank(logQueryVO.getHandlerIp())) {
            queryWrapper.like("handler_ip", logQueryVO.getHandlerIp());
        }

        if (StringUtils.isNotBlank(logQueryVO.getStartTime())) {
            String[] time = logQueryVO.getStartTime().split(",");
            if (time.length == Constants.NUM_TWO) {
                queryWrapper.between("create_time", DateUtils.str2Date(time[0]), DateUtils.str2Date(time[1]));
            }
        }

        if (StringUtils.isNotBlank(logQueryVO.getHandleElapsedTime())) {
            String[] spendTimeList = StringUtils.split(logQueryVO.getHandleElapsedTime(), Constants.SYMBOL_UNDERLINE);
            if (spendTimeList.length == Constants.NUM_TWO) {
                queryWrapper.between("handle_elapsed_time", Integer.valueOf(spendTimeList[0]), Integer.valueOf(spendTimeList[1]));
            }
        }

        queryWrapper.eq("log_status", LogStatus.NORMAL_USE.getStatus());
        queryWrapper.orderByDesc("create_time");
        List<LogEntity> logEntities = logDao.selectList(queryWrapper);
        if (logEntities == null) return ResultEntity.error();
        List<LogVO> collect = logEntities.stream().map((item) -> {
            LogVO target = new LogVO();
            BeanUtils.copyProperties(item, target);
            return target;
        }).collect(Collectors.toList());
        return ResultEntity.ok().put("data", collect);
    }

    @Override
    public ResultEntity deleteBatchLogByIds(Integer[] logIds) {
        boolean res = logDao.updateStatusByIds(logIds, LogStatus.DELETED.getStatus());
        if (res) {
            redisService.del(RedisConstants.CACHE_ADMIN_HANDLER_LOG_LIST);
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

}